package main

import (
	"go-kvsql/bytes_coder"
	"go-kvsql/db/mysqldb"
	"go-kvsql/db/pebbledb"
	"log"
	"runtime"
	"strconv"
	"time"
)

var beginIndex = 1
var maxCount = runtime.NumCPU() * 2 * 10_000

var db, err = pebbledb.NewDefaultPebbleDB("./data/user_info")

type UserInfo struct {
	Id        int64
	Name      string
	FirstName string
	Age       int
	Passwd    string
	Created   time.Time
	Updated   time.Time
}

func insert(id int64) {
	userInfo := &UserInfo{}
	userInfo.Id = id
	userInfo.Name = "trueboy"
	userInfo.Age = 39
	userInfo.Passwd = "123456"
	userInfo.FirstName = "true"
	userInfo.Created = time.Now()
	userInfo.Updated = userInfo.Created

	valueBytes, err := bytes_coder.EncodeJsonBytes(userInfo)
	if err != nil {
		log.Fatal("bytes_coder.EncodeJsonBytes(userInfo)-error:", err)
	}
	err = db.Set([]byte(strconv.FormatInt(userInfo.Id, 10)), valueBytes)
	if err != nil {
		log.Fatal("db.Set-error: ", err)
	}
}

func batchInsert() {
	for i := beginIndex; i <= maxCount; i++ {
		insert(int64(10_0000_0000 + i))
	}
}

func getMysqldb() *mysqldb.MysqlDb {
	username := "root"
	password := "trueboy01!"
	host := "192.168.1.199"
	port := 3306
	dbName := "test"
	data, err := mysqldb.NewMysqlData(username, password, host, port, dbName)
	if err != nil {
		log.Fatalln(err)
	}
	return data
}

func main() {
	batchInsert()
	//fullSynchronizer := full_synchronizer.Execute(getMysqldb(), "user_info", db.List(), &UserInfo{})
	//
	//begin := time.Now().UnixMicro()
	//err := fullSynchronizer.FullSync()
	//if err != nil {
	//	fmt.Println(err)
	//} else {
	//	fmt.Println("successed.")
	//}
	//end := time.Now().UnixMicro()
	//use := end - begin
	//speed := float64(maxCount) * 1000 / float64(use)
	//fmt.Println("count:", maxCount)
	//fmt.Println("use:", use, "us")
	//fmt.Println("speed:", speed, "/ms")

}
